﻿using System.Reflection;
using Abp.AutoMapper;
using Abp.Modules;
using Castle.MicroKernel.Registration;

namespace JHT.SimpleTaskSystem
{

    [DependsOn(typeof(SimpleTaskSystemCoreModule),typeof(AbpAutoMapperModule))]
    public class SimpleTaskSystemApplicationModule : AbpModule
    {
        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());

            // 注册IDtoMapping相关的类
            IocManager.IocContainer.Register(
                Classes.FromAssembly(Assembly.GetExecutingAssembly())
                .BasedOn<IDtoMapping>()
                .WithService.Self()
                .WithService.DefaultInterfaces()
                .LifestyleTransient());

            // [DependsOn(typeof(AbpAutoMapperModule))] 这里需要加上这个属性，不然不会加载AbpAutoMapperModule

            Configuration.Modules.AbpAutoMapper().Configurators.Add(cfg =>
            {
                // 通过依赖注入获取实现了IDtoMapping接口的类
                var mappers = IocManager.IocContainer.ResolveAll<IDtoMapping>();
                foreach (var item in mappers)
                {
                    item.CreateMapping(cfg);// 创建匹配
                }
            });
        }
    }
}
